//=--------------------------------------------------------------------------=
//  (C) Copyright 1996-1999 Microsoft Corporation. All Rights Reserved.
//=--------------------------------------------------------------------------=


// 
// NOTE: 
// Some of the code in this file is MFC implementation specific.
// Changes in future versions of MFC implementation may require
// the code to be changed. Please check the readme of this
// sample for more information 
// 

#include "stdafx.h"
#undef AFX_DATA
#define AFX_DATA AFX_DATA_IMPORT


// NOTE: This line is a hardcoded reference to an MFC header file
//  this path may need to be changed to refer to the location of VC5 install
//  for successful compilation.


#include <..\src\occimpl.h>
#undef AFX_DATA
#define AFX_DATA AFX_DATA_EXPORT
#include "custsite.h"



BEGIN_INTERFACE_MAP(CCustomControlSite, COleControlSite)
	INTERFACE_PART(CCustomControlSite, IID_IDocHostUIHandler, DocHostUIHandler)
END_INTERFACE_MAP()

	

ULONG FAR EXPORT  CCustomControlSite::XDocHostUIHandler::AddRef()
{
	METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
	return pThis->ExternalAddRef();
}


ULONG FAR EXPORT  CCustomControlSite::XDocHostUIHandler::Release()
{                            
    METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
	return pThis->ExternalRelease();
}

HRESULT FAR EXPORT  CCustomControlSite::XDocHostUIHandler::QueryInterface(REFIID riid, void **ppvObj)
{
	METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
    HRESULT hr = (HRESULT)pThis->ExternalQueryInterface(&riid, ppvObj);
	return hr;
}

// * CImpIDocHostUIHandler::GetHostInfo
// *
// * Purpose: Called at initialization
// *
HRESULT FAR EXPORT  CCustomControlSite::XDocHostUIHandler::GetHostInfo( DOCHOSTUIINFO* pInfo )
{
	METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
	
	IDocHostUIHandler* p = pThis->GetDocHostUIHandler();
	if (p)
		return p->GetHostInfo(pInfo);

    return S_OK;
}

// * CImpIDocHostUIHandler::ShowUI
// *
// * Purpose: Called when MSHTML.DLL shows its UI
// *
HRESULT FAR EXPORT  CCustomControlSite::XDocHostUIHandler::ShowUI(
				DWORD dwID, 
				IOleInPlaceActiveObject * pActiveObject,
				IOleCommandTarget * pCommandTarget,
				IOleInPlaceFrame * pFrame,
				IOleInPlaceUIWindow * pDoc)
{

	METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)

	IDocHostUIHandler* p = pThis->GetDocHostUIHandler();
	if (p)
		return p->ShowUI(dwID, pActiveObject, pCommandTarget, pFrame, pDoc);

	return S_OK;
}

// * CImpIDocHostUIHandler::HideUI
// *
// * Purpose: Called when MSHTML.DLL hides its UI
// *
HRESULT FAR EXPORT  CCustomControlSite::XDocHostUIHandler::HideUI(void)
{
	METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)

	IDocHostUIHandler* p = pThis->GetDocHostUIHandler();
	if (p)
		return p->HideUI();

    return S_OK;
}

// * CImpIDocHostUIHandler::UpdateUI
// *
// * Purpose: Called when MSHTML.DLL updates its UI
// *
HRESULT FAR EXPORT  CCustomControlSite::XDocHostUIHandler::UpdateUI(void)
{
	METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
	
	IDocHostUIHandler* p = pThis->GetDocHostUIHandler();
	if (p)
		return p->UpdateUI();

	return S_OK;
}

// * CImpIDocHostUIHandler::EnableModeless
// *
// * Purpose: Called from MSHTML.DLL's IOleInPlaceActiveObject::EnableModeless
// *
HRESULT FAR EXPORT  CCustomControlSite::XDocHostUIHandler::EnableModeless(BOOL fEnable)
{
	METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)

	IDocHostUIHandler* p = pThis->GetDocHostUIHandler();
	if (p)
		return p->EnableModeless(fEnable);

    return E_NOTIMPL;
}

// * CImpIDocHostUIHandler::OnDocWindowActivate
// *
// * Purpose: Called from MSHTML.DLL's IOleInPlaceActiveObject::OnDocWindowActivate
// *
HRESULT FAR EXPORT  CCustomControlSite::XDocHostUIHandler::OnDocWindowActivate(BOOL fActivate)
{
	METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
	IDocHostUIHandler* p = pThis->GetDocHostUIHandler();
	if (p)
		return p->OnDocWindowActivate(fActivate);

    return E_NOTIMPL;
}

// * CImpIDocHostUIHandler::OnFrameWindowActivate
// *
// * Purpose: Called from MSHTML.DLL's IOleInPlaceActiveObject::OnFrameWindowActivate
// *
HRESULT FAR EXPORT  CCustomControlSite::XDocHostUIHandler::OnFrameWindowActivate(BOOL fActivate)
{
	METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)

	IDocHostUIHandler* p = pThis->GetDocHostUIHandler();
	if (p)
		return p->OnFrameWindowActivate(fActivate);

    return E_NOTIMPL;
}

// * CImpIDocHostUIHandler::ResizeBorder
// *
// * Purpose: Called from MSHTML.DLL's IOleInPlaceActiveObject::ResizeBorder
// *
HRESULT FAR EXPORT  CCustomControlSite::XDocHostUIHandler::ResizeBorder(
				LPCRECT prcBorder, 
				IOleInPlaceUIWindow* pUIWindow,
				BOOL fRameWindow)
{
	METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)

	IDocHostUIHandler* p = pThis->GetDocHostUIHandler();
	if (p)
		return p->ResizeBorder(prcBorder, pUIWindow, fRameWindow);

    return E_NOTIMPL;
}

// * CImpIDocHostUIHandler::ShowContextMenu
// *
// * Purpose: Called when MSHTML.DLL would normally display its context menu
// *
HRESULT FAR EXPORT  CCustomControlSite::XDocHostUIHandler::ShowContextMenu(
				DWORD dwID, 
				POINT* pptPosition,
				IUnknown* pCommandTarget,
				IDispatch* pDispatchObjectHit)
{
	METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)

	IDocHostUIHandler* p = pThis->GetDocHostUIHandler();
	if (p)
		return p->ShowContextMenu(dwID, pptPosition, pCommandTarget, pDispatchObjectHit);

    return S_OK; // We've shown our own context menu. MSHTML.DLL will no longer try to show its own.
}

// * CImpIDocHostUIHandler::TranslateAccelerator
// *
// * Purpose: Called from MSHTML.DLL's TranslateAccelerator routines
// *
HRESULT FAR EXPORT  CCustomControlSite::XDocHostUIHandler::TranslateAccelerator(LPMSG lpMsg,
            /* [in] */ const GUID __RPC_FAR *pguidCmdGroup,
            /* [in] */ DWORD nCmdID)
{
	METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)

	IDocHostUIHandler* p = pThis->GetDocHostUIHandler();
	if (p)
		return p->TranslateAccelerator(lpMsg, pguidCmdGroup, nCmdID);

	return S_FALSE;
}

// * CImpIDocHostUIHandler::GetOptionKeyPath
// *
// * Purpose: Called by MSHTML.DLL to find where the host wishes to store 
// *	its options in the registry
// *
HRESULT FAR EXPORT  CCustomControlSite::XDocHostUIHandler::GetOptionKeyPath(BSTR* pbstrKey, DWORD dw)
{

	METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)
		
	IDocHostUIHandler* p = pThis->GetDocHostUIHandler();
	if (p)
		return p->GetOptionKeyPath(pbstrKey, dw);

	return E_NOTIMPL;
}

STDMETHODIMP CCustomControlSite::XDocHostUIHandler::GetDropTarget( 
            /* [in] */ IDropTarget __RPC_FAR *pDropTarget,
            /* [out] */ IDropTarget __RPC_FAR *__RPC_FAR *ppDropTarget)
{
	METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)

	IDocHostUIHandler* p = pThis->GetDocHostUIHandler();
	if (p)
		return p->GetDropTarget(pDropTarget, ppDropTarget);

    return E_NOTIMPL;
}


STDMETHODIMP CCustomControlSite::XDocHostUIHandler::GetExternal( 
            /* [out] */ IDispatch __RPC_FAR *__RPC_FAR *ppDispatch)
{
	// return the IDispatch we have for extending the object Model
	METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)

	IDocHostUIHandler* p = pThis->GetDocHostUIHandler();
	if (p)
		return p->GetExternal(ppDispatch);

	return E_NOTIMPL;
}
        
STDMETHODIMP CCustomControlSite::XDocHostUIHandler::TranslateUrl( 
            /* [in] */ DWORD dwTranslate,
            /* [in] */ OLECHAR __RPC_FAR *pchURLIn,
            /* [out] */ OLECHAR __RPC_FAR *__RPC_FAR *ppchURLOut)
{
	METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)

	IDocHostUIHandler* p = pThis->GetDocHostUIHandler();
	if (p)
		return p->TranslateUrl(dwTranslate, pchURLIn, ppchURLOut);

	return E_NOTIMPL;
}
        
STDMETHODIMP CCustomControlSite::XDocHostUIHandler::FilterDataObject( 
            /* [in] */ IDataObject __RPC_FAR *pDO,
            /* [out] */ IDataObject __RPC_FAR *__RPC_FAR *ppDORet)
{
	METHOD_PROLOGUE(CCustomControlSite, DocHostUIHandler)

	IDocHostUIHandler* p = pThis->GetDocHostUIHandler();
	if (p)
		return p->FilterDataObject(pDO, ppDORet);

	return E_NOTIMPL;
}


IDocHostUIHandler* CCustomControlSite::GetDocHostUIHandler()
{
	if (m_pHostUIHandler)
		return m_pHostUIHandler;

	::SendMessage(::GetParent(m_hWnd), WM_USER + 0x100, (WPARAM)&m_pHostUIHandler, NULL);
	return m_pHostUIHandler;
}